from flask import Blueprint, request, abort

from .. import controller as ctrl
from ..controller import getfn
from ..processor.constants import RANK_SHOOTER, RANK_ASSIST

api = Blueprint('api', __name__)


"""用户基本资料
"""
@api.route('/users', methods=['POST'])
def users():
    fn = getfn(ctrl.UserControllerObj, request.method)
    if request.method == 'POST':
        kwargs = request.json or {}
        return fn(**kwargs)
    abort(500)


@api.route('/users/<uuid:userID>', methods=['GET', 'PATCH'])
def user(userID):
    fn = getfn(ctrl.UserControllerObj, request.method)
    if request.method == 'GET':
        return fn(userID)
    elif request.method == 'PATCH':
        kwargs = request.json or {}
        return fn(userID, **kwargs)
    abort(500)


"""俱乐部基本资料
"""
@api.route('/clubs', methods=['POST', 'GET'])
def clubs():
    fn = getfn(ctrl.ClubControllerObj, request.method)
    if request.method == 'POST':
        kwargs = request.json or {}
        return fn(**kwargs)
    elif request.method == 'GET':
        return fn()
    abort(500)


@api.route('/clubs/<uuid:clubID>', methods=['GET', 'PATCH'])
def club(clubID):
    fn = getfn(ctrl.ClubControllerObj, request.method)
    if request.method == 'GET':
        return fn(clubID)
    elif request.method == 'PATCH':
        kwargs = request.json or {}
        return fn(clubID, **kwargs)
    abort(500)


"""俱乐部/组合接口
"""
@api.route('/app/clubs')
def app_club_index():
    fn = getfn(ctrl.ClubIndexControllerObj, request.method)
    if request.method == 'GET':
        return fn()
    abort(500)


"""俱乐部荣誉
"""
@api.route('/clubs/<uuid:clubID>/honors', methods=['POST', 'GET'])
def honors(clubID):
    fn = getfn(ctrl.HonorControllerObj, request.method)
    if request.method == 'POST':
        kwargs = request.json or {}
        return fn(clubID, **kwargs)
    elif request.method == 'GET':
        return fn(clubid=clubID)
    abort(500)


@api.route('/clubs/<uuid:clubID>/honors/<uuid:honorID>',
    methods=['GET', 'PATCH', 'DELETE'])
def honor(clubID, honorID):
    fn = getfn(ctrl.HonorControllerObj, request.method)
    if request.method == 'GET':
        return fn(clubID, honorID)
    elif request.method == 'PATCH':
        kwargs = request.json or {}
        return fn(clubID, honorID, **kwargs)
    elif request.method == 'DELETE':
        return fn(clubID, honorID)
    abort(500)


"""俱乐部成员
"""
@api.route('/clubs/<uuid:clubID>/members', methods=['POST', 'GET'])
def members(clubID):
    fn = getfn(ctrl.MemberControllerObj, request.method)
    if request.method == 'POST':
        kwargs = request.json or {}
        return fn(clubID, **kwargs)
    elif request.method == 'GET':
        return fn(clubid=clubID)
    abort(500)


@api.route('/clubs/<uuid:clubID>/members/<uuid:userID>',
    methods=['GET', 'PATCH', 'DELETE'])
def member(clubID, userID):
    fn = getfn(ctrl.MemberControllerObj, request.method)
    if request.method == 'GET':
        return fn(clubID, userID)
    elif request.method == 'PATCH':
        kwargs = request.json or {}
        return fn(clubID, userID, **kwargs)
    elif request.method == 'DELETE':
        return fn(clubID, userID)
    abort(500)


"""关注
"""
@api.route('/followers', methods=['POST'])
@api.route('/followers/follower/<uuid:follower>')
@api.route('/followers/owner/<uuid:owner>')
def followers(follower=None, owner=None):
    fn = getfn(ctrl.FollowerControllerObj, request.method)
    if request.method == 'POST':
        kwargs = request.json or {}
        return fn(**kwargs)
    elif request.method == 'GET':
        kwargs = {}
        if follower is not None:
            kwargs['follower'] = follower
        if owner is not None:
            kwargs['owner'] = owner
        return fn(**kwargs)
    abort(500)


@api.route('/followers/follower/<uuid:follower>/owner/<uuid:owner>',
    methods=['GET', 'DELETE'])
@api.route('/followers/owner/<uuid:owner>/follower/<uuid:follower>',
    methods=['GET', 'DELETE'])
def follower(follower, owner):
    fn = getfn(ctrl.FollowerControllerObj, request.method)
    if request.method == 'GET':
        return fn(follower, owner)
    elif request.method == 'DELETE':
        return fn(follower, owner)
    abort(500)


"""联赛基本数据
"""
@api.route('/leagues', methods=['POST', 'GET'])
def leagues():
    fn = getfn(ctrl.LeagueControllerObj, request.method)
    if request.method == 'POST':
        kwargs = request.json or {}
        return fn(**kwargs)
    elif request.method == 'GET':
        return fn()
    abort(500)


@api.route('/leagues/<uuid:leagueID>', methods=['GET', 'PATCH'])
def league(leagueID):
    fn = getfn(ctrl.LeagueControllerObj, request.method)
    if request.method == 'GET':
        return fn(leagueID)
    elif request.method == 'PATCH':
        kwargs = request.json or {}
        return fn(leagueID, **kwargs)
    abort(500)


"""联赛球队
"""
@api.route('/leagues/<uuid:leagueID>/teams', methods=['POST', 'GET'])
def teams(leagueID):
    fn = getfn(ctrl.TeamControllerObj, request.method)
    if request.method == 'POST':
        kwargs = request.json or {}
        return fn(leagueID, **kwargs)
    elif request.method == 'GET':
        return fn(leagueid=leagueID)
    abort(500)


@api.route('/leagues/<uuid:leagueID>/teams/<uuid:clubID>',
    methods=['GET', 'PATCH', 'DELETE'])
def team(leagueID, clubID):
    fn = getfn(ctrl.TeamControllerObj, request.method)
    if request.method == 'GET':
        return fn(leagueID, clubID)
    elif request.method == 'PATCH':
        kwargs = request.json or {}
        return fn(leagueID, clubID, **kwargs)
    elif request.method == 'DELETE':
        return fn(leagueID, clubID)
    abort(500)


"""名单
"""
@api.route('/leagues/<uuid:leagueID>/playerlists/<uuid:clubID>',
    methods=['POST', 'GET'])
def playerlists(leagueID, clubID):
    fn = getfn(ctrl.PlayerListControllerObj, request.method)
    if request.method == 'POST':
        kwargs = request.json or {}
        return fn(leagueID, clubID, **kwargs)
    elif request.method == 'GET':
        return fn(leagueid=leagueID, clubid=clubID)
    abort(500)


@api.route('/leagues/<uuid:leagueID>/playerlists/<uuid:clubID>/player/<uuid:userID>',
    methods=['GET', 'PATCH', 'DELETE'])
def playerlist(leagueID, clubID, userID):
    fn = getfn(ctrl.PlayerListControllerObj, request.method)
    if request.method == 'GET':
        return fn(leagueID, clubID, userID)
    elif request.method == 'PATCH':
        kwargs = request.json or {}
        return fn(leagueID, clubID, userID, **kwargs)
    elif request.method == 'DELETE':
        return fn(leagueID, clubID, userID)
    abort(500)


"""球赛基本数据
"""
@api.route('/matches', methods=['POST'])
def matches():
    fn = getfn(ctrl.MatchControllerObj, request.method)
    if request.method == 'POST':
        kwargs = request.json or {}
        return fn(leagueID, clubID, **kwargs)
    abort(500)


@api.route('/matches/<uuid:matchID>', methods=['GET', 'PATCH', 'DELETE'])
def match(matchID):
    fn = getfn(ctrl.MatchControllerObj, request.method)
    if request.method == 'GET':
        return fn(matchID)
    elif request.method == 'PATCH':
        kwargs = request.json or {}
        return fn(matchID, **kwargs)
    elif request.method == 'DELETE':
        return fn(matchID)
    abort(500)


@api.route('/matches/league/<uuid:leagueID>')
@api.route('/matches/league/<uuid:leagueID>/club/<uuid:clubID>')
@api.route('/leagues/<leagueID>/schedules')
def matchesforleague(leagueID, clubID=None):
    """params - request.args
    """
    fn = getfn(ctrl.MatchControllerObj, request.method)
    if request.method == 'GET':
        kwargs = {'leagueid': leagueID}
        if clubID is not None:
            kwargs['clubid'] = clubID
        if request.args:
            kwargs['params'] = request.args
        return fn(**kwargs)
    abort(500)


@api.route('/matches/club/<uuid:clubID>')
def matchesforclub(clubID):
    """params - request.args
    """
    fn = getfn(ctrl.MatchControllerObj, request.method)
    if request.method == 'GET':
        kwargs = {'clubid': clubID}
        if request.args:
            kwargs['params'] = request.args
        return fn(**kwargs)
    abort(500)


"""出场阵容
"""
@api.route('/matches/<uuid:matchID>/lineups', methods=['POST', 'GET'])
def lineups(matchID):
    fn = getfn(ctrl.LineUpControllerObj, request.method)
    if request.method == 'POST':
        kwargs = request.json or {}
        return fn(matchID, **kwargs)
    elif request.method == 'GET':
        return fn(matchid=matchID)
    abort(500)


@api.route('/matches/<uuid:matchID>/lineups/club/<uuid:clubID>')
def lineupsforclub(matchID, clubID):
    fn = getfn(ctrl.LineUpControllerObj, request.method)
    if request.method == 'GET':
        return fn(matchid=matchID, clubid=clubID)
    abort(500)


@api.route('/matches/<uuid:matchID>/lineups/user/<uuid:userID>',
    methods=['GET', 'PATCH', 'DELETE'])
def lineup(matchID, userID):
    fn = getfn(ctrl.LineUpControllerObj, request.method)
    if request.method == 'GET':
        return fn(matchID, userID)
    elif request.method == 'PATCH':
        kwargs = request.json or {}
        return fn(matchID, userID, **kwargs)
    elif request.method == 'DELETE':
        return fn(matchID, userID)
    abort(500)


"""球赛事件
"""
@api.route('/matches/<uuid:matchID>/events', methods=['POST', 'GET'])
def events(matchID):
    fn = getfn(ctrl.EventControllerObj, request.method)
    if request.method == 'GET':
        return fn(matchID=matchID)
    abort(500)


@api.route('/events/<uuid:eventID>', methods=['GET', 'PATCH', 'DELETE'])
def event(eventID):
    fn = getfn(ctrl.EventControllerObj, request.method)
    if request.method == 'GET':
        return fn(eventID=eventID)
    elif request.method == 'PATCH':
        kwargs = request.json or {}
        return fn(eventID=eventID, **kwargs)
    elif request.method == 'DELETE':
        return fn(eventID=eventID)
    abort(500)


@api.route('/matches/<uuid:matchID>/events/club/<uuid:clubID>')
def eventsforclub(matchID, clubID):
    fn = getfn(ctrl.EventControllerObj, request.method)
    if request.method == 'GET':
        return fn(matchID=matchID, clubID=clubID)
    abort(500)


@api.route('/matches/<uuid:matchID>/events/user/<uuid:userID>')
def eventsforuser(matchID, userID):
    fn = getfn(ctrl.EventControllerObj, request.method)
    if request.method == 'GET':
        return fn(matchID=matchID, userID=userID)
    abort(500)


"""球风记分牌
"""
@api.route('/spiritborads', methods=['POST'])
def spiritborads():
    return 'spiritborads'


"""积分榜
"""
@api.route('/tables/<uuid:leagueID>')
def tables(leagueID):
    fn = getfn(ctrl.TableScoreControllerObj, request.method)
    return fn(leagueID=leagueID)


"""球风榜
"""
@api.route('/spirits/<uuid:leagueID>')
def spirits(leagueID):
    fn = getfn(ctrl.SpiritControllerObj, request.method)
    return fn(leagueid=leagueID)


"""排行榜-射手榜
"""
@api.route('/ranks/league/<uuid:leagueID>/shooters')
def shooters(leagueID):
    fn = getfn(ctrl.RankControllerObj, request.method)
    return fn(leagueid=leagueID, type_=RANK_SHOOTER)


"""排行榜-助攻榜
"""
@api.route('/ranks/league/<uuid:leagueID>/assists')
def assists(leagueID):
    fn = getfn(ctrl.RankControllerObj, request.method)
    return fn(leagueid=leagueID, type_=RANK_ASSIST)


"""排行榜-红黄牌榜
"""
@api.route('/ranks/league/<uuid:leagueID>/rycards')
def rycards(leagueID):
    fn = getfn(ctrl.RankOfryCardControllerObj, request.method)
    return fn(leagueid=leagueID)

"""图片基本资料
@api.route('/image', methods=['POST', 'GET'])
def images():
    fn = getfn(ctrl.ImageObj, request.method)
    return fn()

@api.route('/image/<id:imageID>', methods=['PATCH', 'GET'])
def image(imageID):
    fn = getfn(ctrl.ImageObj, request.method)
    return fn(imageid = imageID)

"""

@api.route('/echo', methods=['POST', 'GET', 'PUT', 'PATCH', 'DELETE'])
def echo():
    fn = getfn(ctrl.UserControllerObj, request.method)

    print("A"*80)
    print(repr(fn))
    print("A"*80)

    try:
        rv = fn()
        print("*"*80)
        print(rv)
        print("*"*80)
        return rv
    except Exception as e:
        print("Exception: %r", e)
        print("Exception: %r", type(e))
    return 'echo'
